import request from "@/utils/request";
import type {
  GetUserListResponse,
  SaveUserParams,
  GetUserRoleListResponse,
} from "./model/userModel";

enum Api {
  getUserListUrl = "/admin/acl/user",
  saveUserUrl = "/admin/acl/user/save",
  getUserRoleListUrl = "/admin/acl/user/toAssign",
  assignUserRoleListUrl = "/admin/acl/user/doAssign",
}

/**
 * 获取用户分页列表
 * @param page 当前页码
 * @param limit 每页条数
 * @param username 用户名
 * @returns GetUserListResponse
 */
export const getUserListApi = (page: number, limit: number, username = "") => {
  return request.get<any, GetUserListResponse>(
    Api.getUserListUrl + `/${page}/${limit}`,
    {
      params: {
        username,
      },
    }
  );
};

/**
 * 添加用户
 * @param data SaveUserParams
 * @returns null
 */
export const saveUserApi = (data: SaveUserParams) => {
  return request.post<any, null>(Api.saveUserUrl, data);
};

/**
 * 获取用户的角色列表
 * @param id 用户id
 * @returns GetUserRoleListResponse
 */
export const getUserRoleListApi = (id: string) => {
  return request.get<any, GetUserRoleListResponse>(
    Api.getUserRoleListUrl + `/${id}`
  );
};

/**
 * 给用户分配角色列表
 * @param userId 用户id
 * @param roleId 角色id
 * @returns null
 */
export const assignUserRoleListApi = (userId: string, roleId: string[]) => {
  return request.post<any, null>(
    Api.assignUserRoleListUrl,
    {},
    {
      params: {
        // 查询字符串的key必须与文档要求一致
        userId,
        // 数组参数在查询字符串中需要处理一下
        roleId: roleId.join(","),
      },
    }
  );
};
